/***************************************************************************
 *   Copyright (C) 2008 by Philipp Müller,                                 *
 *                 2004-2005 Chris Tallon, Andreas Vogel                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#ifndef IOSTREAM_H
#define IOSTREAM_H

#include <QIODevice>
#include "vdrthread.h"

/**
 @author Philipp Müller <philipp.mueller@gmx.de>
*/

class IOStream : public QIODevice
{
        Q_OBJECT

    public:
        IOStream( const QString &filePath, quint32 channel, VDR *vdr, StreamType streamType, QObject *parent = 0 );
        ~IOStream();

        bool seek( qint64 pos );
        bool isSequential() const;
        bool waitForReadyRead( int msecs );
        qint64 pos() const;
        qint64 size() const;
        qint64 bytesAvailable() const;

        StreamType streamType() { return m_streamType; };
        bool streamStarted() { return m_streamStarted; };

    protected:
        qint64 readData( char *data, qint64 maxSize );
        qint64 writeData( const char *data, qint64 maxSize );  //unsupported

    public slots:
        void streamDataReadyRead();

    private:
        VDR *m_vdr;
        qint64 m_fileLength;
        qint64 m_streamPos;
        bool m_sequential;

        /** Don't emit readyRead, when the slot is already called */
        bool m_readyReadRequested;

        /** Has the stream started */
        bool m_streamStarted;

        QString m_filePath;
        quint32 m_channel;
        StreamType m_streamType;
        qint64 m_readyReadSize;
};

#endif
